//
//  MoveCarManager.swift
//  carcareIOS
//
//  Created by ileo on 16/9/18.
//  Copyright © 2016年 chezheng. All rights reserved.
//

import Foundation
import UIKit

class MoveCarManager : BaseManager {
    
    static let sharedInstance = MoveCarManager()
    
    var qrs: [MoveCarQRModel]?
    
    var setting: MoveCarSettingModel?
    
    private override init() {
        super.init()
        self.initStatus = .initing;
        self.updateQRList(success: { [weak self] in
            self?.initStatus = .initSuccess
            }, fail: {[weak self] in
                self?.initStatus = .initFail
            })
        self.updateNoBotherInfo()
    }
    
// MARK: - setget
    
    var viewController: UIViewController {
        get{
            let viewModel:MineMoveCardViewModel = MineMoveCardViewModel()
            let viewController:MineMoveCardViewController = MineMoveCardViewController(viewModel:viewModel).navSetupBackItem(withIdentification:BACK_GRAY_ICON)
            
            return viewController
        }
    }
    
    
// MARK: - func
    
    //更新设置
    public func updateNoBotherInfo () {
        NetMoveCar.getNoBotherInfo(success: { [weak self] (model) in
            self?.setting = model
            }, fail:{})
    }
    
    //更新QR
    public func updateQRList(success:@escaping (()->Void), fail:@escaping (()->Void)) {
        NetMoveCar.getQRList(success: { [weak self] (models) in
            self?.qrs = models
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "move_car_qrs_update"), object: nil)
            success()
            }, fail: {() in
                fail()
            })
    }
    
    private var typeID:Int? = 0
    //随机生成挪车卡
    public func randomQRType(qrURL:String?, success:@escaping ((MoveCarTypeQRModel?)->Void), fail:@escaping (()->Void)) {
        if (qrURL == nil) {
            fail()
            return
        }
        NetMoveCar.getRandomQRTypeInfo(currentTypeID: self.typeID!, success: {[weak self] (model) in
            self?.typeID = model.id?.intValue;
            MoveCarManager.qrType(qrURL: qrURL, qrTypeModel: model, success: { (model) in
                success(model)
                }, fail: {
                    fail()
            })
            }, fail: {() in
                fail()
        })
    }
    
    //生成
    class func qrType(qrURL:String?, qrTypeModel:MoveCarTypeQRModel?, success:@escaping ((MoveCarTypeQRModel?)->Void), fail:@escaping (()->Void)){
        if (qrURL == nil || qrTypeModel == nil) {
            fail()
            return
        }
        UIImage.cacheImage(withURL:qrTypeModel!.imgUrl, finish:{ image in
            autoreleasepool {
                let qrImage:UIImage? = MoveCarManager.createQR(message: qrURL)
                let waterImage:UIImage? = MoveCarManager.createWaterQR(qrImage: qrImage)
                let point: CGPoint = CGPoint(x:CGFloat(qrTypeModel!.leftPadding!.floatValue), y:CGFloat(qrTypeModel!.topPadding!.floatValue))
                let typeImage:UIImage? = MoveCarManager.createTypeQR(typeImage: image, qrPoint: point, qrImage: waterImage)
                qrTypeModel!.showQRImage = typeImage;
                success(qrTypeModel)
            }
        })
    }
    
// MARK: - image
    
    private class func createQR(message: String?) -> UIImage? {
        if (message == nil) {
            return nil
        }
        return ImageHelper.qrImage(for: message, size: 614)
    }
    
    private class func createWaterQR(qrImage: UIImage?) -> UIImage? {
        if (qrImage == nil) {
            return nil
        }
        UIGraphicsBeginImageContextWithOptions(qrImage!.size, false, 0)
        qrImage!.draw(in: CGRect(x: 0, y: 0, width: qrImage!.size.width, height: qrImage!.size.height))
        let width : CGFloat = 104
        let gap : CGFloat =  (qrImage!.size.width - width) / 2
        UIImage(named: "move_car_qr_logo")?.draw(in: CGRect(x: gap, y: gap, width: width, height: width))
        let new : UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return new
    }
    
    private class func createTypeQR(typeImage: UIImage?, qrPoint: CGPoint, qrImage: UIImage?) -> UIImage? {
        if (typeImage == nil || qrImage == nil) {
            return nil
        }
        UIGraphicsBeginImageContextWithOptions(typeImage!.size, false, 0)
        typeImage!.draw(in: CGRect(x: 0, y: 0, width: typeImage!.size.width, height: typeImage!.size.height))
        qrImage!.draw(in: CGRect(x: qrPoint.x, y: qrPoint.y, width: qrImage!.size.width, height: qrImage!.size.height))
        let new : UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return new
    }
    
    class func createPrintQR(typeImage: UIImage?) -> UIImage? {
        
        if (typeImage == nil){
            return nil
        }
        
        let ratio: CGFloat = __SCREEN_IS_5_5 ? (1.0/3.0) : (1.0/2.0)
        
        let maxWidth: CGFloat = 2480 * ratio
        let maxHeight: CGFloat = 3508 * ratio
        
        let width: CGFloat = typeImage!.size.width * ratio
        let height: CGFloat = typeImage!.size.height * ratio
        
        let maxGapLeft: CGFloat = (maxWidth - width) / 2
        let maxGapTop: CGFloat = (maxHeight - height) / 2
        
        UIGraphicsBeginImageContextWithOptions(CGSize(width: 2480 * ratio, height: 3508 * ratio), false, 0)
        typeImage!.draw(in: CGRect(x: maxGapLeft, y: maxGapTop, width: width, height: height))
        
        let context: CGContext = UIGraphicsGetCurrentContext()!;
        
        context.setStrokeColor(UIColor(hexString:"#bfbfbf").cgColor);
        context.setFillColor(UIColor.white.cgColor);
        context.setLineWidth(5 * ratio);
        
        context.setLineDash(phase: 0,lengths:[20 * ratio,10 * ratio])
        context.move(to: CGPoint(x: 520 * ratio, y: maxGapTop))
        context.addLine(to: CGPoint(x: 1960 * ratio, y: maxGapTop))
        
        context.move(to: CGPoint(x: maxGapLeft, y: 930 * ratio))
        context.addLine(to: CGPoint(x: maxGapLeft, y: 2578 * ratio))
        
        context.move(to: CGPoint(x: 520 * ratio, y: maxHeight - maxGapTop))
        context.addLine(to: CGPoint(x: 1960 * ratio, y: maxHeight - maxGapTop))
        
        context.move(to: CGPoint(x: maxWidth - maxGapLeft, y: 930 * ratio))
        context.addLine(to: CGPoint(x: maxWidth - maxGapLeft, y: 2578 * ratio))
        
        context.strokePath()
        
        UIImage(named: "move_car_print_cut")?.draw(in :CGRect(x: 1840 * ratio, y: maxGapTop - 28 * ratio, width: 56 * ratio, height: 56 * ratio))
        
        let new: UIImage? = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();

        return new;
    }
    
    
}

